import omsRequest from "/utils/omsRequest";

export const provinceCityArea = {
  data: {
    citys: [],
    visibleProvinceCityPicker: false
  },
  didMount() {
    this.getCityData({
      areaId: 1,
      type: 0
    }).then(res => {
      this.setData({
        citys: res.voList
      });
      this.syncLoadData({
        areaId: res.voList[0].id,
        type: res.voList[0].type + 1
      }, { provinceIndex: 0 })
    });
  },
  methods: {
    // 根据省获取城市列表
    syncLoadData(params, { provinceIndex, cityIndex }) {
      if (provinceIndex >= 0 && !cityIndex && !this.data.citys[provinceIndex].children) {
        // 获取城市
        this.getCityData(params).then(res => {
          this.data.citys[provinceIndex].children = res.voList;
          this.setData({
            citys: this.data.citys
          });
          this.getCityData({
            areaId: res.voList[0].id,
            type: res.voList[0].type + 1
          }).then(res => {
            this.data.citys[provinceIndex].children[0].children = res.voList;
            this.setData({
              citys: this.data.citys
            })
          });
        });
      }
      if (provinceIndex >= 0 && cityIndex >= 0 && !this.data.citys[provinceIndex].children[cityIndex].children) {
        // 获取地区
        this.getCityData({
          areaId: this.data.citys[provinceIndex].children[cityIndex].id,
          type: this.data.citys[provinceIndex].children[cityIndex].type + 1
        }).then(res => {
          this.data.citys[provinceIndex].children[cityIndex].children = res.voList;
          this.setData({
            citys: this.data.citys
          });
        });
      }
    },
    // 根据城市获取地区列表
    getCityData(params) {
      return new Promise((resolve, reject) => {
        omsRequest({
          url: "/ajax/area/select",
          data: params
        }).then(res => {
          if (res.success) {
            resolve(res);
          } else {
            reject(res);
          }
        }).catch(err => {
          reject(err);
        })
      });
    },
    onProvinceCityShow() {
      this.setData({
        visibleProvinceCityPicker: true
      });
    },
    onProvinceCityClose() {
      this.setData({
        visibleProvinceCityPicker: false
      });
    },
  }
}

export const salerSelect = {
  async didMount() {
    await omsRequest({
      url: "/ajax/user/org",
      data: {
        departmentId: 113682313
      }
    }).then(async (res) => {
      this.setData({
        orgList: res.voList
      });
      // 当前表单未选机构和人员 当前用户是销售人员默认选中
      if (!this.data.form.dingOrgId && !this.data.form.dingUserId) {
        let dingOrgIndex = res.voList.findIndex(item => item.id === +this.data.userInfo.dingOrgId);
        if (dingOrgIndex >= 0) {
          const dingUserRes = await this.onDingOrgChange({ detail: { value: dingOrgIndex } });
          let dingUserIndex = dingUserRes.voList.findIndex(item => item.userid === this.data.userInfo.dingUserId)
          this.onDingUserChange({ detail: { value: dingUserIndex } })
        }
      }
    });
  }
}